package com.seven.tools.page;

import lombok.Data;

import java.util.Collection;
import java.util.Collections;
import java.util.List;


public class PageUtils {

    public static <T> Page<T> createPage(Integer current, Integer size, List<T> list) {
        Page<T> tPage = new Page<>();
        int pages = (list.size() + size - 1) / size;
        int start;
        int end;
        if (size * (current - 1) < 0) {
            start = 0;
        } else if (current > pages) {
            start = list.size();
        } else {
            start = size * (current - 1);
        }
        if (size * current >= list.size()) {
            end = list.size();
        } else {
            end = size * current;
        }
        System.out.println(start + "-->" + end);
        List<T> ts = list.subList(start, end);
        tPage.setRecords(ts);
        tPage.setCurrent(current);
        tPage.setSize(size);
        tPage.setTotal(list.size());
        tPage.setPages(pages);
        return tPage;
    }

    @Data
    public static
    class Page<T> {
        private Collection<T> records = Collections.emptyList();
        private Integer total;
        private Integer current = 1;
        private Integer size = 10;
        private Integer pages;
    }
}


